<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2019-11-28 00:19:28
 * Desc:
 */

namespace app\core\util;

use app\core\cache\{RedisCache, RedisKeys};

class TokenUtil
{
    public function __construct()
    {
        RedisCache::selectDB(config('redis_db_index.login_token'));
    }

    public static function getRequestToken()
    {
        $token = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
        return $token;
    }

    public static function getUserIdByToken()
    {
        $token = self::getRequestToken();
        $payload = JwtUtil::verifyToken($token);
        return $payload['user_id'] ?? 0;

    }


    public static function getAdminIdByToken()
    {
        $token = self::getRequestToken();
        $payload = JwtUtil::verifyToken($token);
        return $payload['admin_id'] ?? 0;

    }

    /**
     * Desc: 设置token副本
     * Date: 2019/11/27 23:43
     * Author: yiyu
     * @param $user_id
     * @param $token
     * @param $expire
     * @return bool
     */
    public static function setApiLoginTokenCopy($user_id, $token, $expire = null)
    {
        if ($token) {
            RedisCache::selectDB(config('redis_db_index.login_token'));
            RedisCache::set(RedisKeys::getAPiLoginTokenCopyStringKey($user_id), $token, $expire);
        }

        return true;
    }

    public static function apiHasLoginOut($user_id)
    {
        if ($user_id) {
            RedisCache::selectDB(config('redis_db_index.login_token'));
            return RedisCache::get(RedisKeys::getAPiLoginTokenCopyStringKey($user_id));
        }

        return false;
    }

    /**
     * Desc: 删除token副本
     * Date: 2019/11/27 23:44
     * Author: yiyu
     * @param $user_id
     * @return bool
     */
    public static function delApiLoginTokenCopy($user_id)
    {
        if ($user_id) {
            RedisCache::selectDB(config('redis_db_index.login_token'));
            RedisCache::del(RedisKeys::getAPiLoginTokenCopyStringKey($user_id));
        }

        return true;
    }


    /**
     * Desc: 设置token副本
     * Date: 2019/11/27 23:43
     * Author: yiyu
     * @param $token
     * @return bool
     */
    public static function setAdminLoginTokenCopy($token)
    {
        if ($token) {
            RedisCache::sAdd(RedisKeys::getAdminLoginTokenCopyKey(), $token);
        }

        return true;
    }

    /**
     * Desc: 判断token是否在集合中
     * Date: 2019/11/27 23:44
     * Author: yiyu
     * @param $token
     * @return bool|mixed
     */
    public static function hasExitAdminLoginTokenCopy($token)
    {
        if ($token) {
            return RedisCache::sIsMember(RedisKeys::getAdminLoginTokenCopyKey(), $token);
        }

        return false;
    }

    /**
     * Desc: 删除token副本
     * Date: 2019/11/27 23:44
     * Author: yiyu
     * @param $token
     * @return bool
     */
    public static function delAdminLoginTokenCopy($token)
    {
        if ($token) {
            RedisCache::sRem(RedisKeys::getAdminLoginTokenCopyKey(), $token);
        }

        return true;
    }

}